//
//  PresentModel.m
//  Zhibo
//
//  Created by apple on 16/6/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "BaseTransitionAnimator.h"


@implementation BaseTransitionAnimator

//动画
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 0.3;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *containerView = transitionContext.containerView;
    
    // For a Presentation:
    //      fromView = The presenting view.
    //      toView   = The presented view.
    // For a Dismissal:
    //      fromView = The presented view.
    //      toView   = The presenting view.
    UIView *fromView;
    UIView *toView;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    } else {
        fromView = fromViewController.view;
        toView = toViewController.view;
    }
    BOOL isPresenting = (toViewController.presentingViewController == fromViewController);
    
    CGRect fromFrame = fromView.frame;
    CGRect toFrame = toView.frame;
    
    if (isPresenting) {
        toFrame = [transitionContext finalFrameForViewController:toViewController];
        // For a presentation, the toView starts off-screen and slides in.
        fromView.frame = fromFrame;
        toView.frame = CGRectOffset(toFrame, toFrame.size.width * -1 * -1, toFrame.size.height * 0 * -1);
    } else {
        fromView.frame = fromFrame;
        toView.frame = toFrame;
    }
    
    if (isPresenting){
        [containerView addSubview:toView];
    }
    else
    {
        [containerView insertSubview:toView belowSubview:fromView];
    }
    NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
    
    [UIView animateWithDuration:transitionDuration  delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        if (isPresenting) {
            fromView.frame = CGRectOffset(fromFrame, - fromFrame.size.width/3.0, 0);
            toView.frame = toFrame;
        } else {
            // For a dismissal, the fromView slides off the screen.
            fromView.frame = CGRectOffset(fromFrame, fromFrame.size.width *1,
                                          fromFrame.size.height * 0);
            toView.frame = CGRectOffset(toFrame,toFrame.size.width/3.0, 0);
        }
        
    } completion:^(BOOL finished) {
        BOOL wasCancelled = [transitionContext transitionWasCancelled];
        
        if (wasCancelled){
            [toView removeFromSuperview];
        }
        // When we complete, tell the transition context
        // passing along the BOOL that indicates whether the transition
        // finished or not.
        [transitionContext completeTransition:!wasCancelled];
    }];
}

@end
